package com.cxh.controller;

import com.cxh.entity.SysUser;
import com.cxh.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Description: <br/>
 * date: 2019-01-28 10:16<br/>
 *
 * @author ChenXh<br   />.
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/login")
    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        return "login";
    }

    @RequestMapping("/toLogin")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        return "1";
    }

    @RequestMapping("/main")
    public String main(HttpServletRequest request, HttpServletResponse response) {
        return "index";
    }

    @RequestMapping("/home")
    public String home(HttpServletRequest request, HttpServletResponse response) {
        return "home";
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public PageInfo<SysUser> getUser(HttpServletRequest request, HttpServletResponse response) {
        PageHelper.startPage(1,2);
        List<SysUser> sysUsers = sysUserService.selectAll();

        sysUserService.loadUser("admin");
        PageInfo<SysUser> sysUserPageInfo = new PageInfo<>(sysUsers);
        return sysUserPageInfo;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(HttpServletRequest request, HttpServletResponse response) {
        SysUser sysUser = new SysUser();
        sysUser.setUserName("陈修浩112");
        sysUser.setLoginName("陈修浩1123");
        sysUserService.addUser(sysUser);
        return "success";
    }

}
